﻿using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using LinCms.Core.Entities;

namespace coreapi.Controllers
{
    [EnableCors("any")]
    [Route("api/[controller]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "v5")]
    public class BeverageInvenController : ControllerBase
    {
        private readonly IFreeSql db;
        public BeverageInvenController(IFreeSql _db)
        {
            this.db = _db;
        }
        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <param name="search"></param>
        /// <returns></returns>
        [HttpGet("GetData")]
        public object GetData(int currentPage = 1, int pageSize = 10, string? search = "")
        {
            int currentPages = currentPage; // 当前页码
            int pageSizes = pageSize; // 每页显示的条目数
            var query = db.Select<Beverage, BeverageInventory>()
                      .InnerJoin((a, b) => a.BeverageID == b.BeverageID)
                      .WhereIf(!string.IsNullOrEmpty(search), (a, b) => a.BeverageName.Contains(search))
                      .ToList((a, b) => new { a.BeverageID, a.BeverageName, a.BeverageType, a.Price, a.Description,b.InventoryID, b.Quantity, b.EntryDate });
            int totalCount = (int)query.Count(); // 总条目数
            var list = query.Skip((currentPages - 1) * pageSize).Take(pageSize).ToList();

            return new
            {
                TotalCount = totalCount,
                Data = list
            };
        }

        /// <summary>
        /// 返回下拉列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetDropdownList")]
        public object GetDropdownList()
        {
            var query = db.Select<Beverage, BeverageInventory>()
                          .LeftJoin((a, b) => a.BeverageID == b.BeverageID)
                          .Where((a, b) => b.InventoryID == null)
                          .ToList((a, b) => new { a.BeverageID, a.BeverageName });

            return query;
        }
        /// <summary>
        /// 添加库存数据
        /// </summary>
        /// <param name="beverage"></param>
        /// <returns></returns>
        [HttpPost]
        public int Post(int BeverageID, BeverageInventory beverage)
        {
            var inventory = new BeverageInventory()
            {
                BeverageID = BeverageID,
                Quantity = beverage.Quantity,
                EntryDate = beverage.EntryDate,
            };
            var affectedRows = db.Insert(inventory).ExecuteAffrows();
            return affectedRows;
        }
        /// <summary>
        /// 编辑库存数据
        /// </summary>
        /// <param name="beverage"></param>
        /// <returns></returns>
        [HttpPut]
        public int Put(int BeverageID, BeverageInventory beverage)
        {          
            var beverage2 = db.Update<BeverageInventory>().Set(a => new BeverageInventory
            {
                Quantity = beverage.Quantity,
                EntryDate = beverage.EntryDate,
            }).Where(a => a.BeverageID == BeverageID).ExecuteAffrows();
            return beverage2;
        }
        /// <summary>
        /// 删除库存数据
        /// </summary>
        /// <param name="InventoryID"></param>
        /// <returns></returns>
        [HttpDelete]
        public int Delete(int InventoryID)
        {
            var beverage2 = db.Delete<BeverageInventory>()
                                .Where(a => a.InventoryID == InventoryID)
                                .ExecuteAffrows();
            return beverage2;
        }
    }
}
